package cn.gailvlun.gll.util;

import android.support.annotation.IntDef;

import com.blankj.utilcode.util.SPUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import cn.gailvlun.gll.net.GsonUtil;



public class SPUtil {

    public final static int USER = 1;
    public final static int GLOBAL = 2;

    @IntDef({USER, GLOBAL})
    @Retention(value = RetentionPolicy.SOURCE)
    private @interface Level {}

    public static void put(@Level int level, String key, boolean value) {
        switch (level) {
            case USER: {
                SPUtils.getInstance().put(ProfileUtil.getUserId() + "-" + key,
                        value);
                break;
            }
            case GLOBAL: {
                SPUtils.getInstance().put(key, value);
                break;
            }
        }
    }

    public static <T> void put(@Level int level, String key, T t) {
        switch (level) {
            case USER: {
                SPUtils.getInstance().put(ProfileUtil.getUserId() + "-" + key, GsonUtil.getGson().toJson(t));
                break;
            }
            case GLOBAL: {
                SPUtils.getInstance().put(key, GsonUtil.getGson().toJson(t));
                break;
            }
        }
    }

    public static <T> void put(@Level int level, String key, int integer) {
        switch (level) {
            case USER: {
                SPUtils.getInstance().put(ProfileUtil.getUserId() + "-" + key, integer);
                break;
            }
            case GLOBAL: {
                SPUtils.getInstance().put(key, integer);
                break;
            }
        }
    }

    public static <T> T get(@Level int level, String key, Class<T> clazz) {
        String result = null;
        switch (level) {
            case USER: {
                result = SPUtils.getInstance().getString(ProfileUtil.getUserId() + "-" + key, "");
                break;
            }
            case GLOBAL: {
                result = SPUtils.getInstance().getString(key, "");
                break;
            }
        }
        if ("".equals(result)) {
            return null;
        }
        return GsonUtil.getGson().fromJson(result, clazz);
    }

    public static boolean getBoolean(@Level int level, String key) {
        switch (level) {
            case USER: {
                return SPUtils.getInstance().getBoolean(ProfileUtil.getUserId() + "-" + key);
            }
            case GLOBAL: {
                return SPUtils.getInstance().getBoolean(key);
            }
            default: {
                return false;
            }
        }
    }

    public static Integer getInteger(@Level int level, String key) {
        switch (level) {
            case USER: {
                return SPUtils.getInstance().getInt(ProfileUtil.getUserId() + "-" + key);
            }
            case GLOBAL: {
                return SPUtils.getInstance().getInt(key);
            }
            default: {
                return null;
            }
        }
    }
}
